#include "stdafx.h"

void Bars::ReqHistoryBars(String ^date)
{
	if (historyBarsID != -1) return;

	TWSLib::IContract ^ic = owner->Account_()->TwsControl()->createContract();

	owner->Account_()->TwsControl()->historicalData += gcnew AxTWSLib::_DTwsEvents_historicalDataEventHandler(this, &Bars::getHistoryBars);
	ic->symbol = owner->Name();
	ic->exchange = "SMART";
	ic->currency = "USD";
	ic->secType = "STK";
	historyBarsID = owner->Account_()->GetNextFreeID();
	owner->Account_()->TwsControl()->reqHistoricalDataEx(historyBarsID, ic, date, "1 D", "1 min", "TRADES", 1, 1);
}

void Bars::ReqHistoryBars()
{
	String ^str = gcnew String( Convert::ToString(DateTime::Today) );

	ReqHistoryBars(str->Substring(6, 4) + str->Substring(3, 2) + str->Substring(0, 2) + " 23:00:00");
}

void Bars::ReqRealTimeBars()
{
	if (realTimeBarsID != -1) return;
	if (requestedHistory == true) currentBar--;

	TWSLib::IContract ^ic = owner->Account_()->TwsControl()->createContract();

	owner->Account_()->TwsControl()->realtimeBar += gcnew AxTWSLib::_DTwsEvents_realtimeBarEventHandler(this, &Bars::getRealTimeBars);
	ic->symbol = owner->Name();
	ic->exchange = "SMART";
	ic->currency = "USD";
	ic->secType = "STK";
	realTimeBarsID = owner->Account_()->GetNextFreeID();
	owner->Account_()->TwsControl()->reqRealTimeBarsEx(realTimeBarsID, ic, 5, "TRADES", 1);
}

void Bars::AddBar(Bar ^bar)
{
	Bars::bar[currentBar] = gcnew Bar;
	Bars::bar[currentBar]->Open = bar->Open;
	Bars::bar[currentBar]->Close = bar->Close;
	Bars::bar[currentBar]->High = bar->High;
	Bars::bar[currentBar]->Low = bar->Low;
	Bars::bar[currentBar]->Time = bar->Time;
	currentBar++;
}

Bars::Bars(Symbol ^owner)
{
	Bars::owner = owner;
	bar = gcnew array<Bar^>(500);
	currentBar = 0;
	historyBarsID = -1;
	realTimeBarsID = -1;
	tmpBarOpen = tmpBarLow = tmpBarHigh = -1;
	requestedHistory = false;
}

Bar^ Bars::operator [](int index)
{
	if (index < 0 || index >= currentBar) return nullptr;

	return bar[index];
}

void Bars::getHistoryBars(System::Object ^sender, AxTWSLib::_DTwsEvents_historicalDataEvent ^e)
{
	if (e->reqId != historyBarsID) return;

	if (e->close == -1)
	{
		requestedHistory = true;
		historyBarsID = -1;
		this->HistoryBarsCompleted();
		return;
	}

	Bar ^bar = gcnew Bar();

	tmpBarOpen = bar->Open = e->open;
	bar->Close = e->close;
	tmpBarHigh = bar->High = e->high;
	tmpBarLow = bar->Low = e->low;

	String^ delimStr = " ";
	array<Char>^ delimiter = delimStr->ToCharArray( );
	array<String^>^ words;
	String^ line = Convert::ToString(e->date);
	words = line->Split( delimiter );

	bar->Time = words[2];
	AddBar(bar);
}

void Bars::getRealTimeBars(System::Object ^sender, AxTWSLib::_DTwsEvents_realtimeBarEvent ^e)
{
	if (e->tickerId != realTimeBarsID) return;

	if (e->high > tmpBarHigh || tmpBarHigh == -1) tmpBarHigh = e->high;
	if (e->low < tmpBarLow || tmpBarLow == -1) tmpBarLow = e->low;

	if (Account::ConvertServerToTime(e->time)->Substring(6,2) == "55")
	{
		Bar ^binfo = gcnew Bar;

		binfo->Open = tmpBarOpen;
		binfo->Close = e->close;
		binfo->High = tmpBarHigh;
		binfo->Low = tmpBarLow;
		binfo->Time = Account::ConvertServerToTime(e->time)->Substring(0, 6) + "00";
		binfo->Time = Convert::ToString(Convert::ToInt16(binfo->Time->Substring(0, 2)) + 2) + binfo->Time->Substring(2, 6);
		AddBar(binfo);
		this->ReadTimeBarAdded(binfo);
	}
	if (Account::ConvertServerToTime(e->time)->Substring(6,2) == "00")
	{
		tmpBarOpen = e->open;
		tmpBarHigh = e->high;
		tmpBarLow = e->low;
	}
}